// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var firstVisited = 0;
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var insuranceState;
	var init = function() {
		firstVisited = base.getUrlParam("firstVisited");
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(8);
		topMenu.init(6);
		router.init();
		$("#leftMenu9 .leftMenu li:first-child").addClass("leftMenu-open");
		$(".footer").removeClass("none");
		insuranceState = base.getUrlParam("insuranceState");
		inquireInsurancePageList();
		initEvent();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
			$(".insurance"+insuranceState).addClass("leftMenu-active");
		}else {
			$(".insurance"+firstVisited).addClass("leftMenu-active");
		}
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		};

		$(".insuranceSearch").unbind("click").click(function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			insuranceState = $(".insuranceState").val();
			inquireInsurancePageList();
		});

		$("#leftMenu9").on("click", ".claimManage", function() {
			router.route("claimManage/claimManage.html");
		});

		$("#leftMenu9").on("click", ".insurance1", function() {
			location.href="../insuranceManage/insuranceManage.html?insuranceState=1";
			$(".insuranceState").val(1);
		});

		$("#leftMenu9").on("click", ".insurance2", function() {
			location.href="../insuranceManage/insuranceManage.html?insuranceState=2";
			$(".insuranceState").val(2);
		});

		$("#leftMenu9").on("click", ".insurance3", function() {
			location.href="../insuranceManage/insuranceManage.html?insuranceState=3";
			$(".insuranceState").val(3);
		});
		
		$("#leftMenu9").on("click", ".insurance4", function() {
			location.href="../insuranceManage/insuranceManage.html?insuranceState=4";
			$(".insuranceState").val(4);
		});
	};

	var inquireInsurancePageList = function() {
		$(".insuranceState").val(insuranceState);
		base.fnAjax({
			action: "inquireInsurancePageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				totalPageCount: totalPageCount,
				totalRecordCount: totalRecordCount,
				insuranceState: insuranceState,
				applicant: $(".applicant").val(),
				dateEnd: $(".dateEnd").val(),
				dateBegin: $(".dateBegin").val(),
				financeNumber: $(".financeNumber").val(),
				insuranceNumber: $(".insuranceNumber").val(),
				coverageName:$(".coverageName").val(),
				insuranceCompany:$(".insuranceCompany").val()
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#insuranceManageTpl").html();
					var html = juicer(tpl, data);
					$("#insuranceManageBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireInsurancePageList();
						});
					$(".detail").unbind("click").click(function() {
						router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init,
		initEvent: initEvent
	};
});